package org.x4444.trans;

import java.io.IOException;
import java.util.Random;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class TransMapper extends Mapper<LongWritable, Text, Text, Text> {

    Text k = new Text();

    WeekPartition weekPart = new WeekPartition();

    Random r = new Random();

    @Override
    protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException,
            InterruptedException {

        String line = value.toString();

        String eventDt = TransUtils.getEventDt(line);
        String oatsProcDt = TransUtils.getOatsProcDt(line);
        String issueSymId = TransUtils.getIssueSymId(line);

        int issueHashCd = TransUtils.getIssueHashCd(issueSymId);
        String weekPartStr = weekPart.evaluate(eventDt);

        StringBuilder sb = new StringBuilder();
        sb.append(weekPartStr);
        sb.append("_");
        sb.append(issueHashCd);
        if (weekPartStr.equals("2012W05")) {
            int rand = r.nextInt(7);
            sb.append("_");
            sb.append(rand);
        }
        sb.append("_");
        sb.append(oatsProcDt);

        k.set(sb.toString());

        context.write(k, value);
    }

    public static void main(String[] args) {

        String res2 = TransUtils.getField("aa|bb|#|dd|ee|ff|gg|hh|ii", 3, '|');
        System.out.println(res2);

        String res3 = TransUtils
                .getEventDt("547677323317|2012-01-31 16:32:49|1466|U1Wme5\\|170|AMZN|2012-01-31 16:32:49|ARBI|CR|N|L|N|32874875|B|700|N|N|I|#|#|U1Wme5170|#|DAY|#|#|#|#|A|177.14000000|N|#|#|#|2012-02-01|O61|#|NNM|#|N|#|20733|#|788|2012-01-31 16:32:48|U1Wme5\\|169|#|#|ARBI|820|N|#|ARBI|820|Chris20120201|#|C|Q,D|#|#|#|#|#|#|#|#|1466|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|F|2012-01-31 16:32:49|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|AMZN|023135106|39673|39673|#|#|547677323313|2012-01-31 16:31:35|547677323315|#|#|AMZN|2012-02-03|2012-02-01");
        System.out.println(res3);

        String res4 = TransUtils
                .getEventDt("548783408155|2012-02-01 15:19:23|15719|ArUr9&0\\\\|EWM|2012-02-01 15:19:23|DEAN|OR|N|M|E|32896216|SL|107|N|N|R|#|#|#|#|DAY|#|#|#|A|A|#|#|#|#|#|2012-02-01|#|#|CQS|#|N|#|22600|#|11672|#|#|#|#|MSSB|13770|N|#|MSSB|13770|DEAN_ROUTE2012020101|#|#|Q,D,N|#|#|#|#|#|2012-02-01 15:19:23|B|#|#|#|M|#|#|#|#|#|#|Y|#|#|#|#|#|#|#|#|M|#|#|#|#|#|F|2012-02-01 15:19:23|2012-02-01 15:19:23|#|Y|107|#|E|500|MSCO|MSCO|1619|EQ20120201151923580|#|#|#|#|#|#|#|EWM|464286830|149777|149777|#|#|548783408155|2012-02-01 15:19:23|#|#|D,Q,B,X,N,P|EWM|2012-02-04|2012-02-01");
        System.out.println(res4);

        String res3o = TransUtils
                .getOatsProcDt("547677323317|2012-01-31 16:32:49|1466|U1Wme5\\|170|AMZN|2012-01-31 16:32:49|ARBI|CR|N|L|N|32874875|B|700|N|N|I|#|#|U1Wme5170|#|DAY|#|#|#|#|A|177.14000000|N|#|#|#|2012-02-01|O61|#|NNM|#|N|#|20733|#|788|2012-01-31 16:32:48|U1Wme5\\|169|#|#|ARBI|820|N|#|ARBI|820|Chris20120201|#|C|Q,D|#|#|#|#|#|#|#|#|1466|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|F|2012-01-31 16:32:49|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|AMZN|023135106|39673|39673|#|#|547677323313|2012-01-31 16:31:35|547677323315|#|#|AMZN|2012-02-03|2012-02-01");
        System.out.println(res3o);

        String res4o = TransUtils
                .getOatsProcDt("548783408155|2012-02-01 15:19:23|15719|ArUr9&0\\\\|EWM|2012-02-01 15:19:23|DEAN|OR|N|M|E|32896216|SL|107|N|N|R|#|#|#|#|DAY|#|#|#|A|A|#|#|#|#|#|2012-02-01|#|#|CQS|#|N|#|22600|#|11672|#|#|#|#|MSSB|13770|N|#|MSSB|13770|DEAN_ROUTE2012020101|#|#|Q,D,N|#|#|#|#|#|2012-02-01 15:19:23|B|#|#|#|M|#|#|#|#|#|#|Y|#|#|#|#|#|#|#|#|M|#|#|#|#|#|F|2012-02-01 15:19:23|2012-02-01 15:19:23|#|Y|107|#|E|500|MSCO|MSCO|1619|EQ20120201151923580|#|#|#|#|#|#|#|EWM|464286830|149777|149777|#|#|548783408155|2012-02-01 15:19:23|#|#|D,Q,B,X,N,P|EWM|2012-02-04|2012-02-01");
        System.out.println(res4o);

        System.out.println("A".hashCode());
        System.out.println("B".hashCode());
        System.out.println("C".hashCode());
        System.out.println("O".hashCode());
        System.out.println("P".hashCode());
        System.out.println("Y".hashCode());
        System.out.println("Z".hashCode());

        System.out.println("Hello World".hashCode());
    }
}
